package com.opensky.qatest.ui.component.common;

import org.openqa.selenium.By;

import com.opensky.qatest.util.common.LogicUtils;
import com.opensky.qatest.util.selenium.WebDriverWrapper;

/*
 * AbstractJoinModal contains base locators, verifications 
 * and some user actions
 *
 * @author: Aliakse_Khursevich (hursevich@gmail.com)
 * @version: 1.1
 * 
 */
public abstract class AbstractJoinModal {

    private static final String JOIN_MODAL_SHOWN = "//div[@id='ui-tooltip-modal' and @aria-hidden='false']";

    private static final String JOIN_MODAL_HIDDEN = "//div[@id='ui-tooltip-modal' and @aria-hidden='true']";

    private static final String NAME_FIELD = "//div[@id='ui-tooltip-modal' and @aria-hidden='false']//input[@id='registration_user_firstName']";

    private static final String EMAIL_FIELD = "//div[@id='ui-tooltip-modal' and @aria-hidden='false']//input[@id='registration_user_email']";

    private static final String PASSWORD_FIELD = "//div[@id='ui-tooltip-modal' and @aria-hidden='false']//input[@id='registration_user_plainPassword']";

    private static final String CATEGORIES = "//div[@id='ui-tooltip-modal' and @aria-hidden='false']//div[@class='category-follow']";

    private static final String START_SHOPPING_BTN = "//div[@id='ui-tooltip-modal' and @aria-hidden='false']//input[@name='submit']";

    private static final String TERMS_LINK = "//div[@id='ui-tooltip-modal' and @aria-hidden='false']//div[@class='terms']";

    private static final String SKIP_LINK = "//div[@id='ui-tooltip-modal' and @aria-hidden='false']//span[@class='modal-skip-link']";

    private static final String MODAL_IMG = "//div[@id='ui-tooltip-modal' and @aria-hidden='false']//div[@class='montage']//img";

    private static final String LOGIN_LINK = "//div[@id='ui-tooltip-modal' and @aria-hidden='false']//a[@class='login']";

    private static final String CLOSE_BTN = "//div[@id='ui-tooltip-modal' and @aria-hidden='false']//div[@class='close-link']";

    private static final String INVALID_EMAIL_ALERT = "//div[@id='ui-tooltip-modal' and @aria-hidden='false']//label[@for='registration_user_email']/span";

    private static final String INVALID_EMAIL_ALERT_MSG = "Please provide valid email address.";

    private static final String EMPTY_PASSWORD_ALERT = "//div[@id='ui-tooltip-modal' and @aria-hidden='false']//label[@for='registration_user_plainPassword']/span";

    private static final String EMPTY_PASSWORD_ALERT_MSG = "Please provide a password.";

    private static final String SHORT_PASSWORD_ALERT = "//div[@id='ui-tooltip-modal' and @aria-hidden='false']//label[@for='registration_user_plainPassword']/span";

    private static final String SHORT_PASSWORD_ALERT_MSG = "Your password is too short. It must have 6 characters or more.";

    private static final String HEALTH_CHECKBOX = "//div[@id='ui-tooltip-modal' and @aria-hidden='false']//input[@id='registration_categoryFollow_healthy-living']";

    private static final String STYLE_CHECKBOX = "//div[@id='ui-tooltip-modal' and @aria-hidden='false']//input[@id='registration_categoryFollow_style']";

    private static final String FOOD_CHECKBOX = "//div[@id='ui-tooltip-modal' and @aria-hidden='false']//input[@id='registration_categoryFollow_food']";

    private static final String DESIGN_CHECKBOX = "//div[@id='ui-tooltip-modal' and @aria-hidden='false']//input[@id='registration_categoryFollow_home-design']";

    protected final WebDriverWrapper driver;

    public AbstractJoinModal(WebDriverWrapper driver) {
	this.driver = driver;
    }

    // ************************ Actions **************************

    public void clickSkip() {
	driver.waitAndClick("SKIP_LINK", By.xpath(SKIP_LINK));
    }

    public void clickClose() {
	driver.waitAndClick("CLOSE_BTN", By.xpath(CLOSE_BTN));
    }

    public void signUp(String email, String password) {
	driver.waitAndSendKeys("EMAIL_FIELD", By.xpath(EMAIL_FIELD), email);
	driver.waitAndSendKeys("PASSWORD_FIELD", By.xpath(PASSWORD_FIELD), password);
	driver.waitAndClick("START_SHOPPING_BTN", By.xpath(START_SHOPPING_BTN));
    }

    public void checkCategories() {
	driver.waitAndClick("HEALTH_CHECKBOX", By.xpath(HEALTH_CHECKBOX));
	driver.waitAndClick("STYLE_CHECKBOX", By.xpath(STYLE_CHECKBOX));
	driver.waitAndClick("FOOD_CHECKBOX", By.xpath(FOOD_CHECKBOX));
	driver.waitAndClick("DESIGN_CHECKBOX", By.xpath(DESIGN_CHECKBOX));
    }

    // ************************ Validations **************************

    public boolean isJoinModalShown() {
	return driver.waitAndVerifyElementPresent("JOIN_MODAL_SHOWN", By.xpath(JOIN_MODAL_SHOWN));
    }

    public boolean isJoinModalHidden() {
	return driver.waitAndVerifyElementPresent("JOIN_MODAL_HIDDEN", By.xpath(JOIN_MODAL_HIDDEN));
    }

    public boolean isJoinModalBaseElementsPresent() {

	return LogicUtils.isAllTrue(driver.waitAndVerifyElementPresent("NAME_FIELD", By.xpath(NAME_FIELD)),
		driver.waitAndVerifyElementPresent("EMAIL_FIELD", By.xpath(EMAIL_FIELD)),
		driver.waitAndVerifyElementPresent("PASSWORD_FIELD", By.xpath(PASSWORD_FIELD)),
		driver.waitAndVerifyElementPresent("CATEGORIES", By.xpath(CATEGORIES)),
		driver.waitAndVerifyElementPresent("START_SHOPPING_BTN", By.xpath(START_SHOPPING_BTN)),
		driver.waitAndVerifyElementPresent("TERMS_LINK", By.xpath(TERMS_LINK)),
		driver.waitAndVerifyElementPresent("SKIP_LINK", By.xpath(SKIP_LINK)),
		driver.waitAndVerifyElementPresent("MODAL_IMG", By.xpath(MODAL_IMG)),
		driver.waitAndVerifyElementPresent("LOGIN_LINK", By.xpath(LOGIN_LINK)),
		driver.waitAndVerifyElementPresent("CLOSE_BTN", By.xpath(CLOSE_BTN)));

    }

    public boolean isInvalidEmailAlertPresent() {
	return driver.waitAndVerifyElementWithTextPresent("INVALID_EMAIL_ALERT", By.xpath(INVALID_EMAIL_ALERT), INVALID_EMAIL_ALERT_MSG);
    }

    public boolean isEmptyPasswordAlertPresent() {
	return driver.waitAndVerifyElementWithTextPresent("EMPTY_PASSWORD_ALERT", By.xpath(EMPTY_PASSWORD_ALERT), EMPTY_PASSWORD_ALERT_MSG);
    }

    public boolean isShortPasswordPresent() {
	return driver.waitAndVerifyElementWithTextPresent("SHORT_PASSWORD_ALERT", By.xpath(SHORT_PASSWORD_ALERT), SHORT_PASSWORD_ALERT_MSG);
    }
}
